package com.hua.ui;

import java.awt.BorderLayout;


import java.awt.CardLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

import com.formdev.flatlaf.FlatLightLaf;
import com.hua.Dao.SqlService;
import com.hua.Model.PermissInfo;
import com.hua.Model.UserInfo;
import com.hua.Service.funtion;

import java.awt.event.ActionListener;
import java.nio.channels.ShutdownChannelGroupException;
import java.sql.SQLException;
import java.awt.event.ActionEvent;

import javax.sound.midi.Soundbank;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Panel;
import java.awt.Label;
import java.awt.FlowLayout;
import java.awt.Font;


public class UI extends JFrame {
	private static UI frame = new UI();
	private JPanel contentPane;
	JPanel panel = new JPanel();
	CardLayout cardLayout = new CardLayout();

	static {
		// 先设置外观，再创建UI。
		// 为了保证创建UI时，已经实现设置好新外观，
		// 最好把设置外观的代码写在静态块中，
		// 并且把这个静态块写在类定义的最前面。
		FlatLightLaf.setup();
		// 主题的切换
		try {
			UIManager.setLookAndFeel(new FlatLightLaf());
		} catch (Exception ex) {
			System.err.println("Failed to initialize LaF");
		}
	}

	// 销毁当前窗口并传递值
	void off(UserInfo user_info, PermissInfo permiss) {
		System.out.println(permiss.getUserID() + "" + permiss.getLogin());
		// 用户ID
		frame.dispose();
		String key=user_info.getKey();
		if (key!=null&&key.equals("admin")) {
			//设置其查看文件为0
			user_info.setId(0);
			System.out.println("管理员登录");
			//权限管理窗口
			PermissManage window;
			try {
				window = new PermissManage();
				window.setVisible(true);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//主界面
			main newWindow = new main(user_info,new PermissInfo(1, 1, 1, 1, 1, 1, 1, 1, 1));
			newWindow.setVisible(true);
		} else {
			if (key!=null&&key.equals("open")) {
				funtion.tips("游客登录", "你可以查看与下载所有的共享文件\n可以上传文件,但上传的文件也将共享\n你不能修改你的信息设置\n否则请登录使用!");
			}
			main newWindow = new main(user_info, permiss);
			newWindow.setVisible(true);
		}

	}

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {

					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	// 弹窗提示层
	public static void tips(String title, String content) {
		JFrame frame = new JFrame("弹窗提示");
		JOptionPane.showMessageDialog(frame, content, title, JOptionPane.INFORMATION_MESSAGE);
	}

	void display(String name) {
		System.out.println("被调用" + name);
		cardLayout.show(panel, name);
	}

	/**
	 * Create the frame.
	 */
	public UI() {
		setTitle("花的WebDisk存储系统");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 787, 485);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		// 面板

		panel.setBounds(0, 28, 769, 410);
		contentPane.add(panel);
		// 给主要显示面板添加布局方式
		panel.setLayout(cardLayout);

		// 创建相应面板类的对象
		login login_ui = new login();
		login_ui.setBounds(0, 0, 915, 464);
		panel.add(login_ui, "p1");

		register register_ui = new register();
		register_ui.setBounds(0, 0, 915, 464);
		panel.add(register_ui, "p2");
		// 普通面板
		Panel panel_1 = new Panel();
		panel_1.setBounds(0, 0, 769, 30);
		contentPane.add(panel_1);
		panel_1.setLayout(null);
		// 顶部菜单栏
		JLabel lblNewLabel = new JLabel("登录");
		lblNewLabel.setFont(new Font("YouYuan", Font.PLAIN, 14));
		lblNewLabel.setBounds(14, 3, 81, 25);
		lblNewLabel.setHorizontalAlignment(SwingConstants.LEFT);
		lblNewLabel.setIcon(new ImageIcon("src/main/resources/images/login.png"));
		panel_1.add(lblNewLabel);

		JLabel lblNewLabel_1 = new JLabel("注册");
		lblNewLabel_1.setIcon(new ImageIcon("src/main/resources/images/register.png"));
		lblNewLabel_1.setHorizontalAlignment(SwingConstants.LEFT);
		lblNewLabel_1.setFont(new Font("YouYuan", Font.PLAIN, 14));
		lblNewLabel_1.setBounds(76, 3, 81, 25);
		panel_1.add(lblNewLabel_1);

		JLabel lblNewLabel_1_1 = new JLabel("帮助");
		lblNewLabel_1_1.setIcon(new ImageIcon("src/main/resources/images/help.png"));
		lblNewLabel_1_1.setHorizontalAlignment(SwingConstants.LEFT);
		lblNewLabel_1_1.setFont(new Font("YouYuan", Font.PLAIN, 14));
		lblNewLabel_1_1.setBounds(142, 3, 81, 25);
		panel_1.add(lblNewLabel_1_1);
		// 面板切换
		lblNewLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				cardLayout.show(panel, "p1");
			}
		});
		lblNewLabel_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				cardLayout.show(panel, "p2");
			}
		});
		//打开帮助
		lblNewLabel_1_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				funtion funtion=new funtion();
				funtion.OpenUrl("https://gitee.com/collectionN/webdisk");
			}
		});
	}
}